<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once "class/Load.class.php";
require_once "class/WowTable.class.php";
require_once "class/ListTable.class.php";
require_once "class/Tooltip.class.php";
require_once "class/Config.class.php";
require_once "class/Inputs.class.php";
require_once "class/StatusRoster.class.php";
require_once "class/SessionRoster.class.php";
require_once "class/Localization.class.php";
require_once "class/CharacterManager.class.php";
require_once "class/LocaleContainer.class.php";
require_once "class/CharacterDisplayer.class.php";
require_once "class/SubscriptionManager.class.php";
require_once "class/TradeskillContainer.class.php";
require_once "class/TradeskillSpecializationContainer.class.php";

// For loading information in footer
Load::GetInstance();

// Outdated characters purge
CharacterManager::GetInstance()->DeleteCharacters(CharacterManager::GetInstance()->GetOutdatedCharactersNames());

// Session initialization
SessionRoster::GetInstance();

if (SessionRoster::GetInstance()->IsConnected() == false) {
	SessionRoster::GetInstance()->Redirect("connection.php");
}

if ( Inputs::GetInstance()->Post('c_password') ) {
	if ( SubscriptionManager::GetInstance()->IsAccountValid(SubscriptionManager::GetInstance()->GetSubscription(SessionRoster::GetInstance()->GetAccountId())->GetName(), Inputs::GetInstance()->Post('oldpass')) == false ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('invalid password') );
	} elseif ( Inputs::GetInstance()->Post('newpass') != Inputs::GetInstance()->Post('confirmpass') ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('the passwords do not match') );
	} elseif ( SubscriptionManager::GetInstance()->CheckPassword(Inputs::GetInstance()->Post('newpass')) == false ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('your password is too short (%s characters min)', SubscriptionManager::GetInstance()->GetPasswordMinimumSize()) );
	} elseif( SubscriptionManager::GetInstance()->UpdatePassword( SessionRoster::GetInstance()->GetAccountId(), Inputs::GetInstance()->Post('newpass') ) == false ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('could not update your password') );
	} else {
		StatusRoster::GetInstance()->AddSuccess( Localization::GetInstance()->Get('password updated successfully') );
		StatusRoster::GetInstance()->AddNote( Localization::GetInstance()->Get('use this password the next time you (...)') );
	}
	
	SessionRoster::GetInstance()->SaveStatus( StatusRoster::GetInstance() );
	SessionRoster::GetInstance()->Redirect("preferences.php");
}

if ( Inputs::GetInstance()->HasGetData('c_del') ) {	
	$character_manager = CharacterManager::GetInstance();
	
	foreach (SubscriptionManager::GetInstance()->GetSubscription( SessionRoster::GetInstance()->GetAccountId() )->GetOwnedCharactersWithTradeskills() as $character) {
		if ( $character->GetId() == Inputs::GetInstance()->Get('id') ) {
			if ($character_manager->DeleteCharacter($character->GetName()) == false) {
				StatusRoster::GetInstance()->AddError(Localization::GetInstance()->Get('character deletion failed'));
			} else {
				StatusRoster::GetInstance()->AddSuccess(Localization::GetInstance()->Get('character successfully delete'));
			}
			
			SessionRoster::GetInstance()->SaveStatus(StatusRoster::GetInstance());
			SessionRoster::GetInstance()->Redirect("preferences.php");
		}
	}
}

if ( Inputs::GetInstance()->HasGetData('t_del') ) {
	$character_manager = CharacterManager::GetInstance();
	$tradeskill_container = TradeskillContainer::GetInstance();
	
	foreach (SubscriptionManager::GetInstance()->GetSubscription( SessionRoster::GetInstance()->GetAccountId() )->GetOwnedCharactersWithTradeskills() as $character) {
		if ( $character->GetId() == Inputs::GetInstance()->Get('id') ) {
			if ($tradeskill_container->HasTradeskillId( Inputs::GetInstance()->Get('t') ) == false) {
				StatusRoster::GetInstance()->AddError(Localization::GetInstance()->Get('this tradeskill does not exist'));
			} elseif ($character_manager->DeleteCharacterTradeskills($character->GetName(), Inputs::GetInstance()->Get('t')) == false) {
				StatusRoster::GetInstance()->AddError(Localization::GetInstance()->Get('tradeskills deletion failed'));
			} else {
				StatusRoster::GetInstance()->AddSuccess(Localization::GetInstance()->Get('tradeskills sucessfully deleted'));
			}
			
			SessionRoster::GetInstance()->SaveStatus(StatusRoster::GetInstance());
			SessionRoster::GetInstance()->Redirect("preferences.php");
		}
	}
}

if ( Inputs::GetInstance()->HasGetData('s_del') ) {
	$character_manager = CharacterManager::GetInstance();
	$tradeskill_specialization_container = TradeskillSpecializationContainer::GetInstance();
	
	foreach (SubscriptionManager::GetInstance()->GetSubscription( SessionRoster::GetInstance()->GetAccountId() )->GetOwnedCharactersWithTradeskills() as $character) {
		if ( $character->GetId() == Inputs::GetInstance()->Get('id') ) {
			if ($tradeskill_specialization_container->HasTradeskillSpecializationId( Inputs::GetInstance()->Get('s') ) == false) {
				StatusRoster::GetInstance()->AddError(Localization::GetInstance()->Get('this tradeskill specialization does not exist'));
			} elseif ($character_manager->DeleteCharacterTradeskillSpecialization($character->GetName(), Inputs::GetInstance()->Get('s')) == false) {
				StatusRoster::GetInstance()->AddError(Localization::GetInstance()->Get('tradeskill specialization deletion failed'));
			} else {
				StatusRoster::GetInstance()->AddSuccess(Localization::GetInstance()->Get('tradeskill specialization sucessfully deleted'));
			}
			
			SessionRoster::GetInstance()->SaveStatus(StatusRoster::GetInstance());
			SessionRoster::GetInstance()->Redirect("preferences.php");
		}
	}
}

if ( Inputs::GetInstance()->HasPostData('del_account') ) {
	if ( SubscriptionManager::GetInstance()->IsAccountValid(SubscriptionManager::GetInstance()->GetSubscription(SessionRoster::GetInstance()->GetAccountId())->GetName(), Inputs::GetInstance()->Post('pass')) == false ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('invalid password') );
	} elseif ( SubscriptionManager::GetInstance()->Delete( SessionRoster::GetInstance()->GetAccountId() ) == false ) {
		StatusRoster::GetInstance()->AddError( Localization::GetInstance()->Get('could not delete account') );
	} else {
		SessionRoster::GetInstance()->Redirect("disconnection.php?acc_del");
	}
	
	SessionRoster::GetInstance()->SaveStatus(StatusRoster::GetInstance());
	SessionRoster::GetInstance()->Redirect("preferences.php");
}

$wow_table = new WowTable(Localization::GetInstance()->Get('your characters'));
$wow_table->AddSubsection();
$wow_table->AddColumn( Localization::GetInstance()->Get('character') );
$wow_table->AddColumn( Localization::GetInstance()->Get('class'), "center", 45 );
$wow_table->AddColumn( Localization::GetInstance()->Get('remove a specialization'), "center", 120 );
$wow_table->AddColumn( Localization::GetInstance()->Get('remove a tradeskill'), null, 400 );
$wow_table->AddColumn( Localization::GetInstance()->Get('delete'), "center" );

foreach (SubscriptionManager::GetInstance()->GetSubscription( SessionRoster::GetInstance()->GetAccountId() )->GetOwnedCharactersWithTradeskills() as $character) {
	$wow_table->AddLine(0, array(
		CharacterDisplayer::GetInstance()->Name($character),
		CharacterDisplayer::GetInstance()->Class_($character),
		CharacterDisplayer::GetInstance()->__DeleteSpecialization($character),
		CharacterDisplayer::GetInstance()->__DeleteTradeskill($character),
		CharacterDisplayer::GetInstance()->__Delete($character),
	));
}

$list_table = new ListTable(Localization::GetInstance()->Get('your statistics'));
$list_table->AddLine(Localization::GetInstance()->Get('number of times you have uploaded'), SubscriptionManager::GetInstance()->GetSubscription( SessionRoster::GetInstance()->GetAccountId() )->GetTimesUploaded());
$list_table->AddLine(Localization::GetInstance()->Get('number of items updated'), SubscriptionManager::GetInstance()->GetSubscription( SessionRoster::GetInstance()->GetAccountId() )->GetNbItemsUpdated().Localization::GetInstance()->Get('item(s)'));
$list_table->AddLine(Localization::GetInstance()->Get('number of spells updated'), SubscriptionManager::GetInstance()->GetSubscription( SessionRoster::GetInstance()->GetAccountId() )->GetNbSpellsUpdated().Localization::GetInstance()->Get('spell(s)'));
$list_table->AddLine(Localization::GetInstance()->Get('number of reagents updated'), SubscriptionManager::GetInstance()->GetSubscription( SessionRoster::GetInstance()->GetAccountId() )->GetNbReagentsUpdated().Localization::GetInstance()->Get('reagent(s)'));
$list_table->AddLine(Localization::GetInstance()->Get('last upload'), SubscriptionManager::GetInstance()->GetSubscription( SessionRoster::GetInstance()->GetAccountId() )->GetLastUpload());

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
	<head>
		<title>LZRoster<?php echo Config::GetInstance()->HasRosterTitle() ?  " - ".Config::GetInstance()->GetRosterTitle() : null; ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/_.css" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/preferences.css" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/listtable.css" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/wowtable.css" />
		<script type="text/javascript" src="js/overlib.js"><!-- overLIB (c) Erik Bosrup --></script>
		<script type="text/javascript" src="js/display.js"></script>
	</head>
	<body>
		
		<?php require_once "inc/head.php"; ?>
		<?php require_once "inc/hmenu.php"; ?>
		
		<h2><?php echo Localization::GetInstance()->Get('preferences'); ?></h2>
		
		<?php
		if (StatusRoster::GetInstance()->HasMessage() or SessionRoster::GetInstance()->HasStatus())
		{
		?>
			
			<p>
				<?php echo Localization::GetInstance()->Get('this page produced some messages. please refer to the following lines'); ?>
			</p>
			
			<?php echo SessionRoster::GetInstance()->HasStatus() ? SessionRoster::GetInstance()->GetStatus()->GetStack_InList() : StatusRoster::GetInstance()->GetStack_InList(); ?>
			
			<p>
				<?php echo Localization::GetInstance()->Get('you can refresh the page %s and try again', "preferences.php"); ?>
			</p>
			
		<?php
		}
		else
		{
		?>
		
		<p id="account">
			<strong><?php echo utf8_encode(SubscriptionManager::GetInstance()->GetSubscription( SessionRoster::GetInstance()->GetAccountId() )->GetNickName()); ?></strong><br />
			<span><?php echo Localization::GetInstance()->Get('subscription'); ?> <?php echo SubscriptionManager::GetInstance()->GetSubscription( SessionRoster::GetInstance()->GetAccountId() )->GetCreationDate(); ?></span>
		</p>
		
		<?php echo $list_table->GetTable(); ?>
		
		<h3><?php echo Localization::GetInstance()->Get('characters bound to this account'); ?></h3>
		
		<p>
			<?php echo Localization::GetInstance()->Get('you an manage your characters by clicking (...)'); ?>
		</p>
		
		<?php $wow_table->GetAndPrintTable(); ?>
		
		<h3><?php echo Localization::GetInstance()->Get('change the language of the roster'); ?></h3>
		<p>
			<?php echo Localization::GetInstance()->Get('current language'); ?> <a href="change_language.php?from=preferences.php"><img src="img/preferences/change_language_off.png" alt="" onmouseover="this.src='img/preferences/change_language_on.png';" onmouseout="this.src='img/preferences/change_language_off.png';" style="vertical-align: middle;" id="img_chg_lang" /><strong style="color: black; margin-left: -120px;" onmouseover="document.getElementById('img_chg_lang').src='img/preferences/change_language_on.png';" onmouseout="document.getElementById('img_chg_lang').src='img/preferences/change_language_off.png';" ><?php echo LocaleContainer::GetInstance()->GetLocale(SessionRoster::GetInstance()->GetLocalization())->GetName(); ?></strong></a>
		</p>
		<p>
			<?php echo Localization::GetInstance()->Get('to change the language of the roster, simply click (...)'); ?>
		</p>
		
		
		<div style="float: left;">
			<h3><?php echo Localization::GetInstance()->Get('change your password'); ?></h3>
			<form action="preferences.php" method="post" class="big_form">
			<p>
				<label for="oldpass">
					<?php echo Localization::GetInstance()->Get('old password'); ?>
				</label>
				<input
					type="password"
					name="oldpass"
					id="oldpass"
				/>
			</p>
			<p>
				<label for="newpass">
					<?php echo Localization::GetInstance()->Get('new password'); ?>
				</label>
				<input
					type="password"
					name="newpass"
					id="newpass"
				/><br />
				
				<label for="confirmpass">
					<?php echo Localization::GetInstance()->Get('confirm new password'); ?>
				</label>
				<input
					type="password"
					name="confirmpass"
					id="confirmpass"
				/>
			</p>
			
			<p class="submit">
				<input type="submit" name="c_password" value="<?php echo Localization::GetInstance()->Get('change'); ?>" />
			</p>
			</form>
		</div>
			
		<div style="float: right;">
			<h3><?php echo Localization::GetInstance()->Get('delete account'); ?></h3>
			<form action="preferences.php" method="post" class="little_form">
			<p>
				<span><?php echo Localization::GetInstance()->Get('password'); ?>:</span>
				<input type="password" name="pass" />
			</p>
			<p class="submit">
				<input type="submit" name="del_account" value="<?php echo Localization::GetInstance()->Get('delete'); ?>" onclick="return confirm('<?php echo Tooltip::GetInstance()->Flatten( Localization::GetInstance()->Get('do you really want to delete this acocunt from the roster? all related data (...)')); ?>');" />
			</p>
			</form>
		</div>
		
		<?php
		}
		?>
		
		<?php require_once "inc/foot.php"; ?>
		
	</body>
</html>